<template>
  <div>
    <el-row type="flex" justify="center">
      <el-col :span="12">
        <el-table :data="cart_list">
          <el-table-column label="商品名" prop="name"></el-table-column>
          <el-table-column label="价格" prop="price"></el-table-column>
          <el-table-column label="数量" prop="num"></el-table-column>
          <el-table-column label="小计">
            <template slot-scope="scope">{{scope.row.price*scope.row.num}}元</template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <span>总额:{{total_amount}}</span>
      </el-col>
    </el-row>
    <el-row type="flex" justify="center">
      <el-col :span="4">
        <template>
          <el-radio v-model="radio" :label="item.coupon.money" v-for="item in coupon_list" :key="item.coupon.id">{{item.coupon.title}}</el-radio>
        </template>
      </el-col>
    </el-row>
    <el-row type="flex" justify="center">
      <el-col >
        <span span="6">实际金额:{{total_amount-radio}}</span>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cart_list: [],
      total_amount: 0,
      coupon_list: [],
      radio:"",
    };
  },
  created() {
    // 向我们的localStorage中读取 购物记录
    const uid = sessionStorage.getItem("uid");
    const cart_list_str = localStorage.getItem(uid);
    //把购物记录字符串 转成JSON对象
    this.cart_list = JSON.parse(cart_list_str);

    //遍历购物列表

    for (let i = 0; i < this.cart_list.length; i++) {
      this.total_amount += this.cart_list[i].price * this.cart_list[i].num;
    }

    //根据用户的id去查找优惠券
    this.axios.get("/coupon?uid="+sessionStorage.getItem("uid")).then(resp=>{
      console.log(resp.data)
      this.coupon_list=resp.data
    }).catch(error=>{
      console.log(error)
    })
  }
};
</script>

<style>
</style>